﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WmsApp.StaticData;
using WmsApp.StaticData.Unity;
using WmsApp.WindowControl;
using WmsApp.WmsService;

namespace WmsApp.Window
{
    public partial class FormInvoiceEditor : DevExpress.XtraEditors.XtraForm,ICanClosing
    {
        /// <summary>
        /// 新增发票构造函数
        /// </summary>
        public FormInvoiceEditor()
        {
            InitializeComponent();
            invoiceBill = InvoiceBill.CreateInvoiceBill();
            //invoiceBill.ExtDateTime1 = DateTime.Now;
            DataBinding();
            try
            {
                //重构后的代码规范，就一句话
                SubjectFactory.AddObserverToSubject(new CustomerObserver("customer", SubjectFactory.GetSubject(), this.lookUpEditProviderCode.Properties, this));
            }
            catch (Exception exception)
            {
               Exceptions.ExceptionHandler.ShowException(exception);
            }
        }

        /// <summary>
        /// 修改发票构造函数
        /// </summary>
        /// <param name="invoiceBill"></param>
        public FormInvoiceEditor(InvoiceBill invoiceBill) 
        {
            InitializeComponent();
            this.invoiceBill = invoiceBill;
            DataBinding();
            this.textEditInvoiceNo.Enabled = false;
        }

        private void DataBinding()
        {
            Lookup.LookupBind.BindingLookUpEditProvider(this.lookUpEditProviderCode,this.textEditProviderName);
            this.textEditInvoiceNo.DataBindings.Add("EditValue", this.invoiceBill, "InvoiceNo");
            this.lookUpEditProviderCode.DataBindings.Add("EditValue", this.invoiceBill, "ProviderCode");
            this.textEditProviderName.DataBindings.Add("EditValue", this.invoiceBill, "ProviderName");
            this.calcEditAmountTax.DataBindings.Add("EditValue", this.invoiceBill, "AmountTax");
            //发票日期
            this.dateEditExtDateTime2.DataBindings.Add("EditValue", this.invoiceBill, "ExtDateTime2",true,DataSourceUpdateMode.OnValidation);
            //发票代码
            this.textEditExtVarChar1.DataBindings.Add("EditValue", this.invoiceBill, "ExtVarChar1");
            //备注
            this.textEditExtVarChar2.DataBindings.Add("EditValue", this.invoiceBill, "ExtVarChar2");
        }

        private InvoiceBill invoiceBill = null;
        public InvoiceBill FInvoiceBill
        {
            get{ return invoiceBill;}
        }

        private void CmdOk_Click(object sender, EventArgs e)
        {
            if(String.IsNullOrEmpty(this.textEditInvoiceNo.Text.Trim()))
            {
                XtraMessageBox.Show("发票号必须输入！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (String.IsNullOrEmpty(this.lookUpEditProviderCode.Text.Trim()))
            {
                XtraMessageBox.Show("供应商必须输入！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (Decimal.Parse(this.calcEditAmountTax.Text.Trim()) == 0)
            {
                XtraMessageBox.Show("金额(含税)必须输入，且必须大于0！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                Factory.WmsServiceFactory.GetServiceClient().SaveInvoiceBill(this.invoiceBill);
                this.DialogResult =System.Windows.Forms.DialogResult.OK;
            }
            catch (Exception ex)
            {
                Exceptions.ExceptionHandler.ShowException(ex);
            }
        }

        private void CmdCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void CalcEditAmountTaxEditValueChanged(object sender, EventArgs e)
        {
            try
            {
                this.textEditAmountTaxChn.Text =
                    (new MoneyToChineseChanger(decimal.Parse(this.calcEditAmountTax.Text))).ToString();
            }
            catch (Exception ex)
            {
                Exceptions.ExceptionHandler.ShowException(ex);
            }
        }
    }
}